
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>通信会话AioSession · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="8-CompletionHandler.html" />
    
    
    <link rel="prev" href="6-AioQuickClient.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../quickStart.html">
            
                <a href="../quickStart.html">
            
                    
                    快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" >
            
                <span>
            
                    
                    源码篇
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="1-ready.html">
            
                <a href="1-ready.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="2-core-interface.html">
            
                <a href="2-core-interface.html">
            
                    
                    核心接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="3-enum.html">
            
                <a href="3-enum.html">
            
                    
                    枚举状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="4-IoServerConfig.html">
            
                <a href="4-IoServerConfig.html">
            
                    
                    服务配置IoServerConfig
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="5-AioQuickServer.html">
            
                <a href="5-AioQuickServer.html">
            
                    
                    服务端AioQuickServer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.6" data-path="6-AioQuickClient.html">
            
                <a href="6-AioQuickClient.html">
            
                    
                    客户端AioQuickClient
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3.7" data-path="7-AioSession.html">
            
                <a href="7-AioSession.html">
            
                    
                    通信会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.8" data-path="8-CompletionHandler.html">
            
                <a href="8-CompletionHandler.html">
            
                    
                    读写回调
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" >
            
                <span>
            
                    
                    进阶篇
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../second/readme.html">
            
                <a href="../second/readme.html">
            
                    
                    开篇概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../second/2-decoder.html">
            
                <a href="../second/2-decoder.html">
            
                    
                    协议解码器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../second/3-type-one.html">
            
                <a href="../second/3-type-one.html">
            
                    
                    一类消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../second/4-type-two.html">
            
                <a href="../second/4-type-two.html">
            
                    
                    二类消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.5" data-path="../second/5-type-three.html">
            
                <a href="../second/5-type-three.html">
            
                    
                    三类消息
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" >
            
                <a target="_blank" href="https://smartboot.github.io/smart-socket/apidocs/index.html">
            
                    
                    API Docs
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-ioc 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    smart-boot 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    mydalgen 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../share/exception/readme.html">
            
                <a href="../../share/exception/readme.html">
            
                    
                    Exception杂谈
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../donation.html">
            
                <a href="../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >通信会话AioSession</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4E03;&#x3001;&#x901A;&#x4FE1;&#x4F1A;&#x8BDD;aiosession">&#x4E03;&#x3001;&#x901A;&#x4FE1;&#x4F1A;&#x8BDD;AioSession</h1>
<blockquote>
<p>AioSession&#x662F;smart-socket&#x4E2D;&#x6700;&#x6838;&#x5FC3;&#x3001;&#x590D;&#x6742;&#x5EA6;&#x6700;&#x9AD8;&#x7684;&#x7C7B;</p>
</blockquote>
<h3 id="&#x6838;&#x5FC3;&#x6210;&#x5458;&#x5C5E;&#x6027;">&#x6838;&#x5FC3;&#x6210;&#x5458;&#x5C5E;&#x6027;</h3>
<table>
<thead>
<tr>
<th>&#x5C5E;&#x6027;&#x540D;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>NEXT_ID</td>
<td>static int</td>
<td>Session ID&#x751F;&#x6210;&#x5668;</td>
</tr>
<tr>
<td>sessionId</td>
<td>final int</td>
<td>Session ID&#xFF0C;&#x503C;&#x53D6;&#x81EA;++NEXT_ID</td>
</tr>
<tr>
<td>status</td>
<td>byte</td>
<td>&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x53D6;&#x503C;&#x8303;&#x56F4;&#xFF1A;SESSION_STATUS_CLOSED(1)&#xFF0C;&#x5904;&#x4E8E;&#x8BE5;&#x72B6;&#x6001;&#x7684;AioSession&#x65E0;&#x6CD5;&#x518D;&#x8FDB;&#x884C;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#xFF1B;SESSION_STATUS_CLOSING(2)&#xFF0C;AioSession&#x72B6;&#x6001;&#x4ECE;SESSION_STATUS_ENABLED&#x5230;SESSION_STATUS_CLOSED&#x7684;&#x8FC7;&#x6E21;&#x72B6;&#x6001;&#x3002;&#x5728;SESSION_STATUS_CLOSING&#x72B6;&#x6001;&#x4E0B;&#xFF0C;AioSession&#x4E0D;&#x63A5;&#x53D7;&#x65B0;&#x7684;&#x8BFB;&#x5199;&#x8BF7;&#x6C42;&#xFF0C;&#x4F46;&#x4F1A;&#x628A;&#x7F13;&#x5B58;&#x4E2D;&#x5F85;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5199;&#x64CD;&#x4F5C;&#xFF0C;&#x8F93;&#x51FA;&#x5B8C;&#x6BD5;&#x540E;&#x66F4;&#x6539;&#x72B6;&#x6001;&#x81F3;SESSION_STATUS_CLOSED&#xFF1B;SESSION_STATUS_ENAB(3)&#xFF0C;AioSessio&#x7684;&#x9ED8;&#x8BA4;&#x72B6;&#x6001;&#xFF0C;&#x8868;&#x793A;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x72B6;&#x6001;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x6B63;&#x5E38;&#x7684;&#x6D88;&#x606F;&#x901A;&#x4FE1;&#x3001;&#x534F;&#x8BAE;&#x7F16;&#x89E3;&#x7801;&#x3001;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x4E1A;&#x52A1;&#x5904;&#x7406;</td>
</tr>
<tr>
<td>attachment</td>
<td>Object</td>
<td>&#x9644;&#x4EF6;&#x5BF9;&#x8C61;</td>
</tr>
<tr>
<td>writeCacheQueue</td>
<td>ArrayBlockingQueue<bytebuffer></bytebuffer></td>
<td>&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x961F;&#x5217;</td>
</tr>
<tr>
<td>serverFlowLimit</td>
<td>Boolean</td>
<td>&#x9650;&#x6D41;&#x6807;&#x5FD7;&#xFF0C;&#x4EC5;&#x670D;&#x52A1;&#x7AEF;&#x6709;&#x503C;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4E3A;null</td>
</tr>
<tr>
<td>readCompletionHandler</td>
<td>ReadCompletionHandler</td>
<td>&#x8BFB;&#x56DE;&#x8C03;</td>
</tr>
<tr>
<td>writeCompletionHandler</td>
<td>WriteCompletionHandler</td>
<td>&#x5199;&#x56DE;&#x8C03;</td>
</tr>
<tr>
<td>channel</td>
<td>AsynchronousSocketChannel</td>
<td>&#x5F53;&#x524D;AioSession&#x6620;&#x5C04;&#x7684;&#x7F51;&#x7EDC;&#x901A;&#x9053;</td>
</tr>
<tr>
<td>semaphore</td>
<td>Semaphore</td>
<td>&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x63A7;&#x5236;&#x8F93;&#x51FA;&#x8D44;&#x6E90;&#x7684;&#x7ADE;&#x4E89;</td>
</tr>
<tr>
<td>ioServerConfig</td>
<td>IoServerConfig</td>
<td>AioQuickClient\AioQuickServer&#x900F;&#x4F20;&#x8FC7;&#x6765;&#x7684;&#x914D;&#x7F6E;&#x9879;</td>
</tr>
</tbody>
</table>
<h3 id="&#x6838;&#x5FC3;&#x65B9;&#x6CD5;">&#x6838;&#x5FC3;&#x65B9;&#x6CD5;</h3>
<table>
<thead>
<tr>
<th>&#x65B9;&#x6CD5;&#x540D;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>AioSession(AsynchronousSocketChannel, IoServerConfig, ReadCompletionHandler, WriteCompletionHandler, boolean serverSession)</td>
<td>&#x552F;&#x4E00;&#x7684;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x65B9;&#x6CD5;</td>
</tr>
<tr>
<td>void readFromChannel()</td>
<td>&#x6570;&#x636E;&#x89E3;&#x7801;&#x2014;&#x2014;&gt;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x2014;&#x2014;&gt;&#x6CE8;&#x518C;&#x8BFB;&#x4E8B;&#x4EF6;</td>
</tr>
<tr>
<td>public void write(final ByteBuffer buffer)</td>
<td>&#x5C06;&#x7F16;&#x7801;&#x540E;&#x7684;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#x5199;&#x5165;&#x7F13;&#x51B2;&#x533A;&#xFF0C;&#x5E76;&#x89E6;&#x53D1;<code>writeToChannel()</code></td>
</tr>
<tr>
<td>void writeToChannel()</td>
<td>&#x5C06;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x6570;&#x636E;&#x5199;&#x5165;&#x81F3;&#x7F51;&#x7EDC;&#x901A;&#x9053;</td>
</tr>
<tr>
<td>public void close(boolean immediate)</td>
<td>&#x5173;&#x95ED;&#x4F1A;&#x8BDD;</td>
</tr>
</tbody>
</table>
<h4 id="1&#x3001;&#x6784;&#x9020;&#x65B9;&#x6CD5;">1&#x3001;&#x6784;&#x9020;&#x65B9;&#x6CD5;</h4>
<pre><code>AioSession(AsynchronousSocketChannel channel, IoServerConfig&lt;T&gt; config, ReadCompletionHandler readCompletionHandler, WriteCompletionHandler writeCompletionHandler, boolean serverSession) {
        this.channel = channel;
        this.readCompletionHandler = readCompletionHandler;
        this.writeCompletionHandler = writeCompletionHandler;
        this.writeCacheQueue = new ArrayBlockingQueue&lt;ByteBuffer&gt;(config.getWriteQueueSize());
        this.ioServerConfig = config;
        this.serverFlowLimit = serverSession ? false : null;
        config.getProcessor().stateEvent(this, StateMachineEnum.NEW_SESSION, null);//&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x673A;
        this.readBuffer = ByteBuffer.allocate(config.getReadBufferSize());
}
</code></pre><ul>
<li>&#x8BE5;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x662F;&#x5305;&#x53EF;&#x89C1;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x53EA;&#x6709;smart-socket&#x624D;&#x53EF;&#x521B;&#x5EFA;AioSession&#x5BF9;&#x8C61;&#x3002;&#x5BF9;&#x8C61;&#x7684;&#x521B;&#x5EFA;&#x5165;&#x53E3;&#x4E3A;<code>AioQuickServer</code>&#x548C;<code>AioQuickClient</code>&#x3002;</li>
<li>&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x7684;5&#x4E2A;&#x5165;&#x53C2;&#x5206;&#x522B;&#x4EE3;&#x8868;&#xFF0C;channe&#xFF1A;&#x5F53;&#x524D;&#x8FDE;&#x63A5;&#x901A;&#x9053;&#x5BF9;&#x8C61;&#xFF1B;config&#xFF1A;AioQuickServer/AioQuickClient&#x670D;&#x52A1;&#x5668;&#x914D;&#x7F6E;&#xFF1B;readCompletionHandler/writeCompletionHandler&#xFF1A;&#x8BFB;&#x5199;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x7C7B;&#xFF1B;serverSession&#xFF1A;true&#x8868;&#x793A;&#x5F53;&#x524D;Session&#x7684;&#x7531;&#x670D;&#x52A1;&#x7AEF;&#x521B;&#x5EFA;&#xFF0C;&#x5426;&#x5219;&#x4E3A;&#x5BA2;&#x6237;&#x7AEF;&#x521B;&#x5EFA;&#x3002;</li>
<li>writeCacheQueue&#xFF0C;&#x521B;&#x5EFA;&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x533A;&#x3002;&#x8BE5;&#x7F13;&#x51B2;&#x533A;&#x5927;&#x5C0F;&#x8BF7;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x5408;&#x7406;&#x7684;&#x503C;&#xFF0C;&#x4EE5;&#x514D;&#x9020;&#x6210;&#x8D44;&#x6E90;&#x6D6A;&#x8D39;&#x3002;</li>
<li>serverFlowLimit&#xFF0C;&#x6D41;&#x63A7;&#x6807;&#x5FD7;&#x3002;&#x6D41;&#x63A7;&#x65B9;&#x6848;&#x4EC5;&#x5B58;&#x5728;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#xFF0C;&#x6240;&#x4EE5;&#x53EA;&#x6709;AioQuickServer&#x6784;&#x5EFA;&#x7684;AioSession&#x5BF9;&#x8C61;&#x4F1A;&#x521D;&#x59CB;&#x5316;&#x8BE5;&#x503C;&#x3002;</li>
<li>&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x673A;<code>StateMachineEnum.NEW_SESSION</code></li>
</ul>
<h4 id="2&#x3001;readfromchannel">2&#x3001;readFromChannel()</h4>
<p>readFromChannel&#x662F;&#x4E13;&#x95E8;&#x7528;&#x4E8E;&#x5904;&#x7406;<a href="http://smartsocket.mydoc.io?v=36765&amp;t=229643" target="_blank">&#x8BFB;&#x56DE;&#x8C03;&#xFF08;ReadCompletionHandler&#xFF09;</a>&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x4F7F;<strong>&#x8BFB;&#x76D1;&#x63A7;-&gt;&#x6570;&#x636E;&#x8BFB;&#x53D6;-&gt;&#x534F;&#x8BAE;&#x89E3;&#x7801;-&gt;&#x4E1A;&#x52A1;&#x5904;&#x7406;-&gt;&#x7EE7;&#x7EED;&#x8BFB;&#x76D1;&#x63A7;</strong> &#x5F62;&#x6210;&#x4E00;&#x4E2A;&#x826F;&#x6027;&#x7684;&#x8FD0;&#x4F5C;&#x72B6;&#x6001;&#x3002;&#x4E14;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x5206;&#x4E3A;&#x4E09;&#x90E8;&#x5206;&#xFF1A;</p>
<ul>
<li>&#x7247;&#x6BB5;&#x4E00;<br>&#x89E3;&#x7801;&#x5DF2;&#x8BFB;&#x53D6;&#x5230;&#x6570;&#x636E;&#xFF0C;&#x82E5;&#x89E3;&#x7801;&#x6210;&#x529F;&#x5219;&#x8FDB;&#x884C;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#xFF0C;&#x76F4;&#x81F3;&#x5F53;&#x524D;&#x5269;&#x4F59;&#x7684;&#x6570;&#x636E;&#x65E0;&#x6CD5;&#x89E3;&#x6790;&#x6210;&#x5B8C;&#x6574;&#x7684;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#x4E3A;&#x6B62;&#x3002;</li>
</ul>
<pre><code>T dataEntry;
while ((dataEntry = ioServerConfig.getProtocol().decode(readBuffer, this, eof)) != null) {
    //&#x5904;&#x7406;&#x6D88;&#x606F;
    try {
        for (Filter&lt;T&gt; h : ioServerConfig.getFilters()) {
            h.processFilter(this, dataEntry);
        }
        ioServerConfig.getProcessor().process(this, dataEntry);
    } catch (Exception e) {
        logger.catching(e);
        for (Filter&lt;T&gt; h : ioServerConfig.getFilters()) {
            h.processFail(this, dataEntry, e);
        }
    }

}
</code></pre><ul>
<li><p>&#x7247;&#x6BB5;&#x4E8C;
&#x901A;&#x8FC7;<strong>&#x7247;&#x6BB5;&#x4E00;</strong>&#x6267;&#x884C;&#x540E;&#x7684;readBuffer&#x6709;&#x4E09;&#x79CD;&#x7ED3;&#x679C;&#xFF1A;1&#x3001;&#x6570;&#x636E;&#x521A;&#x597D;&#x5B8C;&#x5168;&#x89E3;&#x6790;&#x5B8C;&#xFF0C;&#x8C03;&#x7528;<code>clear()</code>&#x91CD;&#x7F6E;&#xFF1B;2&#x3001;&#x5B8C;&#x6210;&#x4E86;&#x89E3;&#x7801;&#x64CD;&#x4F5C;&#xFF0C;&#x4F46;&#x8FD8;&#x6B8B;&#x7559;&#x90E8;&#x5206;&#x672A;&#x89E3;&#x6790;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x5C06;&#x5269;&#x4F59;&#x7684;&#x6570;&#x636E;&#x8F6C;&#x79FB;&#x81F3;ByteBuffer&#x7684;&#x5934;&#x90E8;&#xFF1B;3&#x3001;&#x539F;&#x5148;&#x5DF2;&#x6709;&#x7684;&#x6570;&#x636E;&#x4E0D;&#x6EE1;&#x8DB3;&#x89E3;&#x7801;&#x6761;&#x4EF6;&#xFF0C;&#x5219;&#x6062;&#x590D;&#x73B0;&#x573A;&#xFF0C;&#x7EE7;&#x7EED;&#x8BFB;&#x3002;</p>
<pre><code>if (readBuffer.remaining() == 0) {
    readBuffer.clear();
} else if (readBuffer.position() &gt; 0) {
    readBuffer.compact();
} else {
    readBuffer.position(readBuffer.limit());
    readBuffer.limit(readBuffer.capacity());
}
</code></pre></li>
<li><p>&#x7247;&#x6BB5;&#x4E09;
&#x5982;&#x679C;&#x89E6;&#x53D1;&#x4E86;&#x6D41;&#x63A7;&#x6761;&#x4EF6;&#xFF0C;&#x5219;&#x8BBE;&#x7F6E;&#x6D41;&#x63A7;&#x6807;&#x5FD7;<code>serverFlowLimit = true</code>&#x3002;&#x5426;&#x5219;&#x7EE7;&#x7EED;&#x65B0;&#x4E00;&#x8F6E;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x4E00;&#x65E6;&#x8BFB;&#x53D6;&#x5230;&#x6570;&#x636E;&#xFF0C;&#x4F1A;&#x7531;aioCompletionHandler&#x518D;&#x6B21;&#x89E6;&#x53D1;<code>readFromChannel()</code>&#x65B9;&#x6CD5;</p>
<pre><code>if (serverFlowLimit != null &amp;&amp; writeCacheQueue.size() &gt; ioServerConfig.getFlowLimitLine()) {
   serverFlowLimit = true;
} else {
   continueRead();
}
</code></pre></li>
</ul>
<h4 id="3&#x3001;writebytebuffer-buffer">3&#x3001;write(ByteBuffer buffer)</h4>
<p>&#x5148;&#x5C06;&#x6570;&#x636E;&#x5B58;&#x5165;&#x7F13;&#x51B2;&#x533A;<code>writeCacheQueue</code>&#x3002;&#x91C7;&#x53D6;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x4FE1;&#x53F7;&#x91CF;<code>semaphore</code>&#xFF0C;&#x83B7;&#x53D6;&#x6210;&#x529F;&#x89E6;&#x53D1;&#x901A;&#x9053;&#x7684;&#x8F93;&#x51FA;&#x64CD;&#x4F5C;&#x3002;&#x6B64;&#x5904;&#x4E4B;&#x6240;&#x4EE5;&#x5F15;&#x5165;&#x4E86;&#x4FE1;&#x53F7;&#x91CF;<code>semaphore</code>&#xFF0C;&#x662F;&#x56E0;&#x4E3A;<code>writeToChannel()</code>&#x53EF;&#x80FD;&#x7531;&#x4E1A;&#x52A1;&#x7EBF;&#x7A0B;&#x901A;&#x8FC7;<code>write</code>&#x8C03;&#x7528;&#xFF0C;&#x4E5F;&#x6709;&#x53EF;&#x80FD;&#x7531;<code>WriteCompletionHandler</code>&#x7684;&#x56DE;&#x8C03;&#x89E6;&#x53D1;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5904;&#x7406;&#x597D;&#x540C;&#x6B65;&#x63A7;&#x5236;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;<code>WritePendingException</code>&#x3002;</p>
<pre><code>public void write(final ByteBuffer buffer) throws IOException {
        if (isInvalid()) {
            return;
        }
        try {
            writeCacheQueue.put(buffer);
        } catch (InterruptedException e) {
            logger.error(e);
        }
        if (semaphore.tryAcquire()) {
            writeToChannel();
        }
    }
</code></pre><h4 id="4&#x3001;writetochannel">4&#x3001;writeToChannel</h4>
<p><code>wrtie</code>&#x53EA;&#x662F;&#x9762;&#x5411;&#x4E1A;&#x52A1;&#x7684;&#x8F93;&#x51FA;&#x884C;&#x4E3A;&#xFF0C;&#x771F;&#x6B63;&#x6267;&#x884C;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x7684;&#x662F;<code>writeToChannel</code>&#x3002;&#x4E2A;&#x4EBA;&#x8BA4;&#x4E3A;&#x8BE5;&#x65B9;&#x6CD5;&#x662F;&#x6574;&#x4E2A;<code>smart-socket</code>&#x590D;&#x6742;&#x5EA6;&#x6700;&#x9AD8;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x9700;&#x8981;&#x5177;&#x5907;&#x4E00;&#x5B9A;&#x7A0B;&#x5EA6;&#x7684;&#x7EBF;&#x7A0B;&#x540C;&#x6B65;&#x77E5;&#x8BC6;&#x624D;&#x80FD;&#x5B8C;&#x5168;&#x7406;&#x89E3;&#x4EE3;&#x7801;&#x610F;&#x4E49;&#x3002;<strong>&#x6267;&#x884C;<code>writeToChannel</code>&#x4E4B;&#x524D;&#xFF0C;&#x5FC5;&#x987B;&#x6301;&#x6709;&#x4FE1;&#x53F7;&#x91CF;<code>semaphore</code></strong></p>
<ul>
<li>&#x7247;&#x6BB5;&#x4E00;<br>&#x82E5;&#x524D;&#x4E00;&#x6B21;&#x8F93;&#x51FA;&#x64CD;&#x4F5C;&#x8FD8;&#x6B8B;&#x7559;&#x90E8;&#x5206;&#x6570;&#x636E;&#xFF0C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x8F93;&#x51FA;&#xFF0C;&#x5426;&#x5219;&#x91CA;&#x653E;&#x5176;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x3002;<pre><code>if (writeAttach.buffer != null &amp;&amp; writeAttach.buffer.hasRemaining()) {
   continueWrite();
   return;
}
writeAttach.buffer = null;//&#x91CA;&#x653E;&#x5BF9;&#x8C61;
</code></pre></li>
<li><p>&#x7247;&#x6BB5;&#x4E8C;<br>&#x5224;&#x65AD;&#x5F53;&#x524D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x5F85;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x82E5;&#x5DF2;&#x65E0;&#x53EF;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;&#x8D44;&#x6E90;&#x3002;&#x4F46;&#x662F;&#x5728;&#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;&#x4E4B;&#x540E;&#x7684;&#x77AC;&#x95F4;&#x53EF;&#x80FD;&#x6709;&#x65B0;&#x7684;&#x6570;&#x636E;&#x8FDB;&#x5165;&#x7F13;&#x51B2;&#x533A;&#xFF0C;&#x56E0;&#x6B64;&#x9700;&#x8981;&#x518D;&#x6B21;&#x4E89;&#x62A2;&#x4FE1;&#x53F7;&#x91CF;&#x8D44;&#x6E90;&#x5E76;&#x91CD;&#x65B0;&#x89E6;&#x53D1;<code>writeToChannel</code>&#x3002;</p>
<pre><code>if (writeCacheQueue.isEmpty()) {
          semaphore.release();
          if (isInvalid()) {//&#x6B64;&#x65F6;&#x53EF;&#x80FD;&#x662F;Closing&#x6216;Closed&#x72B6;&#x6001;
              close();
          } else if (writeCacheQueue.size() &gt; 0 &amp;&amp; semaphore.tryAcquire()) {
              writeToChannel();
          }
          return;
}
</code></pre></li>
<li><p>&#x7247;&#x6BB5;&#x4E09;<br>&#x538B;&#x7F29;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x5F85;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E14;&#x6700;&#x5927;&#x7684;&#x538B;&#x7F29;&#x5B57;&#x8282;&#x957F;&#x5EA6;&#x4E3A;<strong>32*1024</strong></p>
<pre><code>Iterator&lt;ByteBuffer&gt; iterable = writeCacheQueue.iterator();
int totalSize = 0;
while (iterable.hasNext() &amp;&amp; totalSize &lt;= MAX_WRITE_SIZE) {
     totalSize += iterable.next().remaining();
}
byte[] data = new byte[totalSize];
int index = 0;
while (index &lt; data.length) {
     ByteBuffer srcBuffer = writeCacheQueue.poll();
     int remain = srcBuffer.remaining();
     srcBuffer.get(data, index, remain);
     index += remain;
}
</code></pre></li>
<li>&#x7247;&#x6BB5;&#x56DB;<br>&#x6784;&#x5EFA;&#x538B;&#x7F29;&#x540E;&#x7684;ByteBuffer&#x5BF9;&#x8C61;&#x6267;&#x884C;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x3002;<pre><code>writeAttach.buffer = ByteBuffer.wrap(data);
continueWrite();
</code></pre></li>
</ul>
<h4 id="5&#x3001;tryreleaseflowlimit">5&#x3001;tryReleaseFlowLimit</h4>
<p>&#x5224;&#x65AD;&#x5F53;&#x524D;AioSession&#x662F;&#x5426;&#x5904;&#x7406;&#x9650;&#x6D41;&#x72B6;&#x6001;&#xFF0C;&#x82E5;&#x7B26;&#x5408;&#x89E3;&#x9664;&#x9650;&#x6D41;&#x6761;&#x4EF6;&#xFF0C;&#x5219;&#x91CA;&#x653E;&#x6D41;&#x63A7;&#xFF0C;&#x5E76;&#x91CD;&#x65B0;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x3002;&#x5904;&#x4E8E;&#x9650;&#x6D41;&#x72B6;&#x6001;&#x7684;AioSession&#x5FC5;&#x7136;&#x4E0D;&#x5B58;&#x5728;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x6240;&#x4EE5;&#x6B64;&#x5904;&#x53EF;&#x653E;&#x5FC3;&#x7684;&#x6267;&#x884C;<code>channel.read();</code></p>
<pre><code>if (serverFlowLimit != null &amp;&amp; serverFlowLimit &amp;&amp; writeCacheQueue.size() &lt; ioServerConfig.getReleaseLine()) {
      serverFlowLimit = false;
      continueRead();
}
</code></pre><h4 id="6&#x3001;close">6&#x3001;close</h4>
<p>&#x5173;&#x95ED;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#xFF0C;&#x5F53;&#x5165;&#x53C2;&#x4E3A;<code>true</code>&#x6216;&#x7F13;&#x51B2;&#x533A;&#x5DF2;&#x6E05;&#x7A7A;&#x65F6;&#xFF0C;&#x76F4;&#x63A5;&#x5173;&#x95ED;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x3002;&#x5426;&#x5219;&#x5207;&#x6362;&#x4F1A;&#x8BDD;&#x72B6;&#x6001;&#x4E3A;<code>SessionStatus.SESSION_STATUS_CLOSING</code>&#x76F4;&#x81F3;&#x7F13;&#x5B58;&#x533A;&#x88AB;&#x6E05;&#x7A7A;&#x518D;&#x6267;&#x884C;&#x5173;&#x95ED;&#x3002;</p>
<pre><code>public void close(boolean immediate) {
    if (status == SESSION_STATUS_CLOSED) {
        logger.warn(&quot;ignore, session:{} is closed:&quot;, getSessionID());//&#x8BF4;&#x660E;close&#x65B9;&#x6CD5;&#x88AB;&#x91CD;&#x590D;&#x8C03;&#x7528;
        return;
    }
    status = immediate ? SESSION_STATUS_CLOSED : SESSION_STATUS_CLOSING;
    if (immediate) {
        try {
            channel.close();
            if (logger.isDebugEnabled()) {
                logger.debug(&quot;session:{} is closed:&quot;, getSessionID());
            }
        } catch (IOException e) {
            logger.catching(e);
        }
        ioServerConfig.getProcessor().stateEvent(this, StateMachineEnum.SESSION_CLOSED, null);
    } else if ((writeBuffer == null || !writeBuffer.hasRemaining()) &amp;&amp; writeCacheQueue.isEmpty() &amp;&amp; semaphore.tryAcquire()) {
        close(true);
        semaphore.release();
    } else {
        ioServerConfig.getProcessor().stateEvent(this, StateMachineEnum.SESSION_CLOSING, null);
    }
}
</code></pre><footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-05-27 14:34:42
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="6-AioQuickClient.html" class="navigation navigation-prev " aria-label="Previous page: 客户端AioQuickClient">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="8-CompletionHandler.html" class="navigation navigation-next " aria-label="Next page: 读写回调">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"通信会话AioSession","level":"1.2.3.7","depth":3,"next":{"title":"读写回调","level":"1.2.3.8","depth":3,"path":"smart-socket/first/8-CompletionHandler.md","ref":"smart-socket/first/8-CompletionHandler.md","articles":[]},"previous":{"title":"客户端AioQuickClient","level":"1.2.3.6","depth":3,"path":"smart-socket/first/6-AioQuickClient.md","ref":"smart-socket/first/6-AioQuickClient.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/docs/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/first/7-AioSession.md","mtime":"2018-05-27T06:34:42.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-23T08:59:34.607Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

